package com.schooleducation.controller.teacher;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.schooleducation.entity.Lesson;
import com.schooleducation.entity.LessonContent;
import com.schooleducation.entity.User;
import com.schooleducation.service.ServiceFactory;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/teacher/publishLessonContent")
public class PublishLessonContentController {

	@Resource(name = "serviceFac")
	private ServiceFactory serviceFac;

	/**
	 * 获取发布课题内容页面
	 * @param model
	 * @param session
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String getPage(Model model, HttpSession session) {
		User teacher = (User) session.getAttribute("teacher");
		if (teacher != null) {
			List<Lesson> list = serviceFac.getLessonService().getLessonListByTeacher(teacher);
			model.addAttribute("teacher", teacher);
			model.addAttribute("lessonList", list);

			return "teacher/publishLessonContent";
		} else {
			return "redirect:../login";
		}
	}

	/**
	 * 提交富文本的内容
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public String publishLessonContent(HttpSession session, String data) {
		Map<String, Object> state = new HashMap<String, Object>();
		User teacher = (User) session.getAttribute("teacher");
		if (teacher != null) {
			state.put("isLogin", "yes");
			LessonContent lc = (LessonContent) JSONObject.toBean(JSONObject.fromObject(data), LessonContent.class);
			if (serviceFac.getLessonService().addLessonContent(lc)) {
				state.put("save", "success");
				state.put("page", "lessonInfo");
			} else {
				state.put("save", "fail");
			}
		} else {
			state.put("isLogin", "no");
		}
		return JSONObject.fromObject(state).toString();
	}
}
